/**
 * 生成随机字符串
 * @param length
 * @param prefix
 * @param suffix
 * @returns {string}
 */
const randomString = (length = 48, prefix = '', suffix = '') => {
  const chars = 'abcdedfghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
  let rs = '';
  let rnd;
  let len = chars.length;

  for (let i = 0; i < length; i++) {
    rnd = parseInt(Math.random() * len);
    rs += chars[rnd];
  }

  return `${prefix}${rs}${suffix}`;
};

const isType = (type) => {
  return (obj) => {
    return Object.prototype.toString.call(obj) === `[object ${type}]`;
  };
};

/**
 * 判断数组
 * @type {function(*=): boolean}
 */
const isArray = isType('Array');
/**
 * 判断数字
 * @type {function(*=): boolean}
 */
const isNumber = isType('Number');
/**
 * 判断字符串
 * @type {function(*=): boolean}
 */
const isString = isType('String');
/**
 * 判断对象
 * @type {function(*=): boolean}
 */
const isObject = isType('Object');
/**
 * 判断function
 * @type {function(*=): boolean}
 */
const isFunction = isType('Function');

const lessThan = (baseNumber) => {
  return (number) => {
    return number < baseNumber;
  };
};

const lessThan10 = lessThan(10);

/**
 * 格式化日期
 * @param date
 * @param reg
 * @returns {string}
 */
const formatDate = (date = new Date(), reg = 'yyyy-MM-dd HH:mm:ss') => {
  date = new Date(date);
  return reg.replace(/yyyy/, date.getFullYear())
    .replace(/MM/, lessThan10(date.getMonth() + 1) ? '0' + (date.getMonth() + 1) : date.getMonth())
    .replace(/dd/, lessThan10(date.getDate()) ? '0' + (date.getDate()) : date.getDate())
    .replace(/HH/, lessThan10(date.getHours()) ? '0' + (date.getHours()) : date.getHours())
    .replace(/mm/, lessThan10(date.getMinutes()) ? '0' + (date.getMinutes()) : date.getMinutes())
    .replace(/ss/, lessThan10(date.getSeconds()) ? '0' + (date.getSeconds()) : date.getSeconds());
};

/** 处理状态 */
const tickeTaskStatusFormatter = function (tickeTaskStatus) {
  let mapObj = {
    maintaining: '待检修',
    trusting: '待委托',
    checking: '待诊断',
    explaining: '待说明',
    assessing: '待评价',
    assessingAgain: '待评价',
    closing: '待关闭',
    accepting: '待验收',
    end: '已完成',
    ignore: '已忽略',
    closed: '已关闭',
    produce: '新产生',
    confirming: '待产生',
    backing: '已退回',
    alertDealing: '报警待处理',
    abnormalDealing: '异常待处理',
    resultAssessing: '结果待评价',
    ending: '已完成'
  };
  return (tickeTaskStatus ? mapObj[tickeTaskStatus] : '') || '';
};

/** 任务来源 */
const alertFromTypeFormatter = function (alertFromType) {
  let mapObj = {
    '1': '在线预警',
    '2': '智能诊断',
    '3': '大数据分析',
    '4': '离线诊断',
    '11': '平台规则预警',
    '12': '过程数据预警',
    '100': '当日点检',
    '110': '精密检测',
    '120': '检修计划',
    '130': '点检异常',
    '140': '备修委托',
    '210': '临时委托',
    '220': '辊道备修',
    '合并告警': '合并告警'
  };
  return (alertFromType ? mapObj[alertFromType + ''] : '') || alertFromType;
};

/** 报警级别 */
const priorityFormatter = function (priority) {
  let res = {
    text: '',
    class: ''
  };
  if (priority == 2 || priority == '注意') {
    res.text = '注意';
    res.class = 'alerts-minor';
  } else if (priority == 3 || priority == '警告') {
    res.text = '警告';
    res.class = 'alerts-critical';
  } else if (priority == 4 || priority == '危险') {
    res.text = '危险';
    res.class = 'alerts-black';
  } else if (priority == 0 || priority == -1 || priority == '正常') {
    res.text = '正常';
    res.class = 'alerts-normal';
  } else {
    res.text = '未知';
    res.class = '';
  }
  return `<span class=${res.class}>${res.text}</span>`;
};

/**
 * 数组去重
 * @param array
 * @returns {any[]}
 */
const removeDuplicated = array => {
  return Array.from(new Set(array.filter(item => item !== null)));
};

/**
 * 将多维数组转成一维数组
 * @param array
 * @returns {*}
 */
const flattenArray = array => {
  return array.reduce((result, item) => {
    return result.concat(Array.isArray(item) ? flattenArray(item) : item);
  }, []);
};

export {
  isArray,
  isNumber,
  isString,
  isObject,
  isFunction,
  formatDate,
  randomString,
  tickeTaskStatusFormatter,
  alertFromTypeFormatter,
  priorityFormatter,
  removeDuplicated,
  flattenArray
};
